package userInterface;

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.*;
import javax.swing.event.*;

import userInterface.Panels.*;

/**
 * Main {@link javax.swing.JFrame JFrame} for displaying the application.
 * @author BDS
 */
public class FrameMain extends JFrame
{
	private static final long serialVersionUID = 1L;

	private ChangeListener tabbedPaneChangeListener;
	
	private JLabel label;

	private int currentIndex;

	/**
	 * Class constructor.
	 */
	public FrameMain()
	{
		tabbedPaneChangeListener = new MainChangeListener();

		currentIndex = -1;

		setTitle("Insim");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/main.png")));
		//setMinimumSize(new Dimension(811, 750));
		setResizable(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Available look and feels:
		// Metal
		// Nimbus
		// CDE\Motif
		// Windows
		// Windows Classic
		try
		{
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
			{
				if (info.getName().equals("Windows"))
				{
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		}
		catch (Exception exception)
		{
			// Dat feel when Metal.
		}

		// Center on the screen.
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		tabbedPane.addChangeListener(tabbedPaneChangeListener);
		tabbedPane.setBorder(null);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);

		JPanel pnlMain = new PanelMain();
		pnlMain.setBorder(null);
		tabbedPane.addTab("Main", pnlMain);
		
		label = new JLabel("Main", new ImageIcon(getClass().getResource("/images/main.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(0, label);

		JPanel pnlSettings = new userInterface.graph.PanelGraph();//PanelSettings();
		pnlSettings.setBorder(null);
		tabbedPane.addTab("Settings", pnlSettings);

		label = new JLabel("Settings", new ImageIcon(getClass().getResource("/images/settings.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(1, label);
		
		JPanel pnlEnvironments = new PanelEnvironments();
		pnlEnvironments.setBorder(null);
		tabbedPane.addTab("Environments", pnlEnvironments);

		label = new JLabel("Environments", new ImageIcon(getClass().getResource("/images/environments.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(2, label);
		
		JPanel pnlWorkflows = new PanelWorkflows();
		pnlWorkflows.setBorder(null);
		tabbedPane.addTab("Workflows", pnlWorkflows);

		label = new JLabel("Workflows", new ImageIcon(getClass().getResource("/images/workflows.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(3, label);
		
		JPanel pnlCommands = new PanelCommands();
		pnlCommands.setBorder(null);
		tabbedPane.addTab("Commands", pnlCommands);

		label = new JLabel("Commands", new ImageIcon(getClass().getResource("/images/commands.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(4, label);
		
		JPanel pnlLog = new PanelLog();
		pnlLog.setBorder(null);
		tabbedPane.addTab("Log", pnlLog);

		label = new JLabel("Log", new ImageIcon(getClass().getResource("/images/log.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(5, label);
		
		JPanel pnlAbout = new PanelAbout();
		pnlAbout.setBorder(null);
		tabbedPane.addTab("About", pnlAbout);
		
		label = new JLabel("About", new ImageIcon(getClass().getResource("/images/about.png")), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		tabbedPane.setTabComponentAt(6, label);
	}

	/**
	 * {@link javax.swing.event.ChangeListener ChangeListener} for {@link userInterface.FrameMain FrameMain} {@link javax.swing.JTabbedPane JTabbedPanes}.
	 * @author BDS
	 */
	private class MainChangeListener implements ChangeListener
	{
		/**
		 * <ul>
		 *    <li>Prompts the current pane to display a save dialog, if required.</li>
		 *    <li>Changes the current tab to the selected tab.</li>
		 * </ul>
		 * @param changeEvent The triggering {@link javax.swing.event.ChangeEvent ChangeEvent}.
		 */
		@Override
		public void stateChanged(ChangeEvent changeEvent)
		{
			if (currentIndex != -1)
			{
				((PanelBase) ((JTabbedPane) changeEvent.getSource()).getComponent(currentIndex)).promptSaveChanges();
			}
			currentIndex = ((JTabbedPane) changeEvent.getSource()).getSelectedIndex();
			
			// HACK The icon/title formatting causes a BasicTabbedPaneUI$TabContainer to be injected into the JTabbedPane components at index 1.
			if (currentIndex > 0)
			{
				currentIndex++;
			}

			((PanelBase) ((JTabbedPane) changeEvent.getSource()).getComponent(currentIndex)).load();
		}
	}
}